/*
 * io.h
 *
 *  Created on: Sep 19, 2013
 *      Author: Ken Arok
 */

#ifndef IO_H_
#define IO_H_

#include <stdint.h>
#include "mydefs.h"
#include "uart.h"
#include "spi.h"


/*! \brief The Enumeration of Port Name.
 *
 * IOP_COM1 - Serial UART RS232 Communication Port 1.
 * IOP_COM2 - Serial UART RS232 Communication Port 2.
 * IOP_SPI1 - Serial SPI Communication port 1.
 * IOP_SPI2 - Serial SPI Communication port 2.
 * IOP_SPI3 - Serial SPI Communication port 3.
 *
 */

/** \name I/O Port Name.
 */
typedef enum {
	IOP_COM1 = 0,	//!< COM1 port
	IOP_COM2,		//!< COM2 port
	IOP_SPI1,		//!< SPI port 1
	IOP_SPI2,		//!< SPI Port 2
	IOP_SPI3,		//!< SPI port 3
} io_port_t;


/*! \brief The Enumeration of I/O Request Code.
 *
 * IORC_SET_DEFAULT - Set parameters of I/O to default.
 * IORC_LOAD_SETTING_FROM_ROM - Load setting parameter of I/O from ROM.
 * IORC_SAVE_SETTING_TO_ROM - Save setting parameter to ROM.
 * IORC_SET_RX_TIMEOUT - Set receive timeout of I/O to given value.
 * IORC_SET_SPEED - Set transfer speed of I/O to given value.
 * IORC_SET_DATA_BIT - Set data length of I/O to given value.
 * IORC_SET_MODE - Set the I/O mode.
 * IORC_SET_TRANSFER_POLLED - Set receive and transmit of I/O driven by Polled.
 * IORC_SET_TRANSFER_POLLED_TX - Set transmit of I/O driven by Polled, receive driven by Interrupt.
 * IORC_SET_TRANSFER_POLLED_RX - Set receive of I/O driven by Polled, transmit driven by Interrupt.
 * IORC_SET_TRANSFER_INTERRUPT - Set receive and transmit of I/O driven by Interrupt.
 * IORC_SET_PARITY -  Set parity of UART I/O. SPI, I2C is ignored.
 * IORC_SET_STOP_BIT -  Set stop bit of UART I/O. SPI, I2C is ignored.
 * IORC_SET_SLAVE_ADDR - Set slave address of I2C Device.
 *
 */

/** \name I/O Request Code.
 */
typedef enum {
	IORC_RESTART_MODULE = 0,
	IORC_SET_DEFAULT,
	IORC_LOAD_SETTING_FROM_ROM,
	IORC_SAVE_SETTING_TO_ROM,
	IORC_SET_RX_TIMEOUT,
	IORC_SET_SPEED,
	IORC_SET_DATA_BIT,
	IORC_SET_MODE,
	IORC_SET_TRANSFER_POLLED,
	IORC_SET_TRANSFER_POLLED_TX,
	IORC_SET_TRANSFER_POLLED_RX,
	IORC_SET_TRANSFER_INTERRUPT,
	IORC_SET_PARITY,
	IORC_SET_STOP_BIT,
	IORC_SET_SLAVE_ADDR,
} io_request_code_t;


/*! \brief Read/Write Flag.
 *
 * IORWF_BYTE_ORDER - Byte order transfer (Data length 8 bit).
 * IORWF_WORD_ORDER - Word order transfer (Data length 16 bit).
 * IORWF_DWORD_ORDER - DWord order transfer (Data length 32 bit).
 *
 */

/** \name I/O Read/Write Flag Enumeration.
 */
typedef enum {
	IORWF_BYTE_ORDER = 0,
	IORWF_WORD_ORDER,
	IORWF_DWORD_ORDER,
} io_rw_flag_t;


/** \brief Initilalize I/O Drivers.
 */
void vIO_Init(void);

/** \brief Control I/O Behavior.
 *
 * \param ioPort		I/O port.
 * \param reqCode		Request Code.
 * \param pValue		Pointer to the value. NULL if no value.
 *
 * \return	status		pdPASS = Success, pdFAIL = failed.
 *
 */
uint8_t vIO_Ctrl(io_port_t ioPort, io_request_code_t reqCode, void *pValue);

/** \brief Open I/O Port.
 *
 * \param ioPort		I/O port.
 * \param flag			Flag of opening.
 *
 * \return	status		pdPASS = Success, pdFAIL = failed.
 *
 */
uint8_t vIO_Open(io_port_t ioPort, uint8_t ioFlag);

/** \brief Close I/O Port.
 *
 * \param ioPort		I/O port.
 *
 * \return	Status		pdPASS = Success; pdFAIL = failed.
 *
 */
uint8_t vIO_Close(io_port_t ioPort);

/** \brief Write Data to I/O.
 *
 * \param ioPort		I/O port (see \ref subsection_io_port_enumeration).
 * \param pBuff			Pointer to the buffer of data to be written.
 * \param size			Size of data to be written.
 * \param flag			Flag of writing operation (see \ref subsection_io_readwrite_flag_enumeration).
 *
 * \return	Status		pdPASS = Success; pdFAIL = failed.
 *
 */
uint8_t vIO_Write(io_port_t ioPort, const void *pBuf, uint32_t size, io_rw_flag_t flag);

/** \brief Read Data from I/O.
 *
 * \param ioPort		I/O port (see \ref subsection_io_port_enumeration).
 * \param pBuff			Pointer to the buffer where data will be stored.
 * \param size			Size of data to be read.
 * \param flag			Flag of writing operation (see \ref subsection_io_readwrite_flag_enumeration).
 *
 * \return Actual number of count of received data.
 *
 */
uint32_t vIO_Read(io_port_t ioPort, void *pBuf, uint32_t size, io_rw_flag_t flag);


#endif /* IO_H_ */
